#import uuid


from common import *

class IndexHandler(pyrestful.rest.RestHandler):

    @get(_path='/', _produces=mediatypes.APPLICATION_JSON)
    def show_index(self):
        try:
            self.set_header('Access-Control-Allow-Origin', '*')
            self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
            self.set_header('Access-Control-Max-Age', 1000)
            self.set_header('Access-Control-Allow-Headers', '*')
            self.set_header('Content-type', 'application/html')
            return self.render('templates/index.html')

        except Exception ,e:
            logging.error(e)
            return {'state' : '500'}


    @post(_path='/save_diffed_html', _types=[str],
            _produces=mediatypes.APPLICATION_JSON)
    def save_html(self, html_body):

        try:
            self.set_header('Access-Control-Allow-Origin', '*')
            self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
            self.set_header('Access-Control-Max-Age', 1000)
            self.set_header('Access-Control-Allow-Headers', '*')
            self.set_header('Content-type', 'application/json')
            with open('handler/templates/index.html', 'w+') as f:
                f.write(html_body)
            return {'state' : '200'}
        except Exception, e:
            logging.error(str(e))
            logging.error('saved differ html erorr')
            return {'state' : '500'}



